﻿using Common;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tools.mysql
{
    public partial class mysqltest : Form
    {
        public mysqltest()
        {
            InitializeComponent();
        }

        private void mysqltest_Load(object sender, EventArgs e)
        {

        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            string connStr = "server=" + txt_add.Text.Trim() + ";user=" + txt_username.Text.Trim() + ";database=" + txt_db.Text.Trim() + ";port=" + txt_port.Text.Trim() + ";password=" + txt_pwd.Text.Trim() + ";";
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {

                app("开始连接....");
                conn.Open();

                conn.Close();
                app("连接成功....");

                DbManager.Ins.ConnStr = connStr;
                if (txt_db.Text.Trim() == "")
                {
                    string sql = "show databases;";
                    List<MySqlParameter> par = new List<MySqlParameter>();

                    //  par.Add(new MySqlParameter("@databases", "databases"));

                    DataTable dt = DbManager.Ins.ExcuteDataTable(sql, par.ToArray());


                    string a = "数据库列表：\r\n";
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow d in dt.Rows)
                        {
                            a += d[0].ToString() + "\r\n";
                        }
                    }
                    app(a);


                }
                else
                {
                    string sql = "use " + txt_db.Text.Trim() +";show tables";
                    List<MySqlParameter> par = new List<MySqlParameter>();

                //  par.Add(new MySqlParameter("@db", txt_db.Text.Trim()));

                    DataTable dt = DbManager.Ins.ExcuteDataTable(sql, par.ToArray());


                    string a = "数据表：\r\n";
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow d in dt.Rows)
                        {
                            a += d[0].ToString() + "\r\n";
                        }
                    }
                    app(a);
                }


            }
            catch (Exception ex)
            {
                app(ex.ToString());
            }

        }



        void app(string str)
        {
            r_mes.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + str + "\r\n");
        }

        private void r_mes_TextChanged(object sender, EventArgs e)
        {
            r_mes.SelectionStart = r_mes.Text.Length;
            r_mes.ScrollToCaret();
        }

        private void r_mes_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            r_mes.Clear();
        }
    }
}
